package models

import(
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql"
)

var (
	db orm.Ormer 
)


func init(){
	orm.Debug = true //开始调试模式
	orm.RegisterDriver("mysql", orm.DRMySQL)
	orm.RegisterDataBase("default", "mysql", "root:@tcp(127.0.0.1:3306)/imooc?charset=utf8&parseTime=True&loc=Local")
	orm.RegisterModel(new(User))
	db  =  orm.NewOrm()
}

type User struct{
	Id int64 
	Name string
	Gender int
	Age int
}

func AddUser(user *User)(int64,error){
	
	Id, err := db.Insert(user)
	return Id,err
}


func GetUsers(users *[]User,limit int,offset int){
	qb, _ := orm.NewQueryBuilder("mysql")
	qb.Select("*").From("user").Where("age = 28").Limit(limit).Offset(offset)
	// 导出SQL语句
	sql := qb.String()
	//
	db.Raw(sql).QueryRows(users)
}

